package ClientPackage;
import java.io.IOException;
import java.net.*;

/**
 * 	This class services the Client for waitting to receive any data packet from the server.
 *  
 */
public class ClientThread implements Runnable {
	private DatagramSocket _clientSocket;
	
	public ClientThread(DatagramSocket datagramSocket){
		_clientSocket = datagramSocket;		
	} //End constructor
	
	public void run() {
		
		while(true){
			byte[] receiveData = new byte[1024]; 
			DatagramPacket receiveMessage = new DatagramPacket(receiveData, receiveData.length);                  
			
			try {
				_clientSocket.receive(receiveMessage); //block operation
				
				String text = new String(receiveMessage.getData());
				int index=0;
				for(; index<text.length();index++){
					if(text.charAt(index)== '\0')
						break;	
				} //End for
				text = text.substring(0, index);
				System.out.println();
				System.out.println(	"INCOMING: "+text);
				System.out.println();
				System.out.println("Please input a command to request(e.g. GREETING or MESSAGE):");		
			} catch (IOException e) {
				System.out.println(	"Error happens when receiving data from server....");
				e.printStackTrace();
			} //End catch
		
		}//End while
	
	} //End run

} //End class receiveThread
